package com.hh.org.controller;

import com.hh.basic.util.AjaxResult;
import com.hh.basic.util.PageList;
import com.hh.org.domain.SdDetail;
import com.hh.org.query.SdDetailQuery;
import com.hh.org.service.ISdDetailService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄浩
 * @create 2022-06-03 17:07
 */
@RestController
@RequestMapping("/sddetail")
public class SdDetailController {
    @Resource
    private ISdDetailService sdDetailService;

    /**
     * 返回分解＋高级查询的结果集
     * @param sdDetailQuery
     * @return
     */
    @PostMapping
    public PageList<SdDetail> loadAll(@RequestBody SdDetailQuery sdDetailQuery){
        return sdDetailService.queryPage(sdDetailQuery);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<SdDetail> queryAll(){
        return sdDetailService.list();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SdDetail queryById(@PathVariable("id") Long id){
        return sdDetailService.getById(id);
    }

    /**
     * 执行新增或修改
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody SdDetail sdDetail){
        try {
            sdDetailService.saveOrUpdate(sdDetail);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }

    /**
     * 根据id删除
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            sdDetailService.removeById(id);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            sdDetailService.removeByIds(ids);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }
}
